package com.ug.system.processor;

import com.ug.common.cache.redis.RedisCacheService;
import com.ug.common.constant.HotConstants;
import com.ug.common.utils.SpringContextHolder;
import com.ug.system.entity.AttributeValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;
import us.codecraft.webmagic.selector.XpathSelector;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author: LY
 * @description: 百度热搜信息获取
 */
public class BaiDuHotHtmlPageProcessor implements PageProcessor {

    private static final Logger logger = LoggerFactory.getLogger(BaiDuHotHtmlPageProcessor.class);
    private final RedisCacheService redisCacheService = SpringContextHolder.getBean(RedisCacheService.class);
    //  部分一：抓取网站的相关配置，包括编码、抓取间隔、重试次数等
    private final Site site = Site.me().setRetryTimes(3).setSleepTime(1000);

    @Override
    public void process(Page page) {
        logger.info("获取百度热搜数据");
        String key = HotConstants.HOT_TYPE_BAIDU.concat(HotConstants.HOT_TYPE_KEY);
        try {
            //  获取html页面元素下的id值为list_1EDla的元素
            Selectable select = page.getHtml().$(".container-bg_lQ801").select(
                    //  匹配所有a标签class='item-wrap_2oCLZ'的元素 即：<a class="item-wrap_2oCLZ"></a>
                    new XpathSelector("div[@class='category-wrap_iQLoo horizontal_1eKyQ']")
            );
            List<AttributeValue> attributeValues = new ArrayList<>();
            //  获取到的多个元素
            List<Selectable> nodes = select.nodes();
            //  循环获取对应标签数据
            for (Selectable node : nodes) {
                //  获取链接
                String link = node.$(".title_dIF3B", "href").get();
                //  获取标题
                String title = node.$(".c-single-text-ellipsis", "text").get();
                //  获取热搜指数
                String value = node.$(".hot-index_1Bl1a", "text").get();
                AttributeValue attributeValue = new AttributeValue(title, link, value);
                attributeValues.add(attributeValue);
            }
            redisCacheService.put(key, attributeValues, HotConstants.HOT_TIME_OUT, TimeUnit.HOURS);
            page.putField("attributeValues", attributeValues);
        }catch (Exception e){
            logger.error("获取百度热搜数据异常：{}", e.getMessage());
            page.putField("attributeValues", new ArrayList<>());
        }
    }

    @Override
    public Site getSite() {
        return site;
    }

}
